package com.example.zhou.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.example.zhou.application.MyApplication;
import com.example.zhou.heplper.ToActivityUtil;
import com.example.zhou.model.LastestNews;
import com.example.zhou.model.StoriesBean;
import com.example.zhou.zhihunews.NewsInfoActivity;
import com.example.zhou.zhihunews.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 11065 on 2016/10/27.
 */

public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Object> list = new ArrayList<>();
    private List<String> list1 = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;

    private int TOP_STORY = 1;
    private int STORARY = 2;
    private int UPDATE = 3;
    public BitmapRequestBuilder<GlideUrl, Bitmap> requestBuilder;

    private OnItemClick onItemClick;


    public NewsAdapter(Context context, List<Object> list, List<String> list1) {

        this.context = context;
        this.list = list;
        this.list1 = list1;
        this.inflater = LayoutInflater.from(context);

        requestBuilder = Glide.with(context)
                .from(GlideUrl.class)
                .asBitmap()
                .animate(android.R.anim.fade_in)
                .centerCrop()
                .error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
    }

    public void update(List<Object> list, List<String> list1) {
        this.list = list;
        this.list1 = list1;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position) instanceof StoriesBean) {

            return STORARY;

        } else if (list.get(position) instanceof LastestNews.TopStoriesBean) {

            return TOP_STORY;
        } else if (list.get(position) instanceof String) {
            return UPDATE;
        }
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == STORARY || viewType == TOP_STORY) {
            View view = inflater.inflate(R.layout.item_news, parent, false);
            NewViewHolder holder = new NewViewHolder(view);

            return holder;
        } else if (viewType == UPDATE) {
            return new FooterHoder(inflater.inflate(R.layout.footer, parent, false));
        }
        return null;
    }

    private OnItemClick click = new OnItemClick() {
        @Override
        public void OnClick(View view, Object o, int position) {
            onItemClick.OnClick(view, o, position);
        }
    };

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TOP_STORY) {
            final LastestNews.TopStoriesBean storiesBean = (LastestNews.TopStoriesBean) list.get(position);

            ((NewViewHolder) holder).tv_title.setText(storiesBean.getTitle());
            ((NewViewHolder) holder).tv_date.setText(list1.get(position));
            requestBuilder.load(new GlideUrl(storiesBean.getImage())).into(((NewViewHolder) holder).iv_title);
           /* ((NewViewHolder) holder).cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("ID", storiesBean.getId() + "");
                    ToActivityUtil.toNextActivity(context, NewsInfoActivity.class, bundle);
                }
            });*/
            ((NewViewHolder) holder).bindView(storiesBean);
            ((NewViewHolder) holder).setOnItemClick(click);

        } else if (getItemViewType(position) == STORARY) {
            final StoriesBean storiesBean = (StoriesBean) list.get(position);
            ((NewViewHolder) holder).tv_title.setText(storiesBean.getTitle());
            ((NewViewHolder) holder).tv_date.setText(list1.get(position));
            requestBuilder.load(new GlideUrl(storiesBean.getImages().get(0))).into(((NewViewHolder) holder).iv_title);
           /* ((NewViewHolder) holder).cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("ID", storiesBean.getId() + "");
                    ToActivityUtil.toNextActivity(context, NewsInfoActivity.class, bundle);
                }
            });*/
            ((NewViewHolder) holder).bindView(storiesBean);
            ((NewViewHolder) holder).setOnItemClick(click);

        }


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class NewViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.iv_title)
        ImageView iv_title;

        @BindView(R.id.cardView)
        CardView cardView;

        private OnItemClick onItemClick;

        public NewViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            cardView.setOnClickListener(this);
        }

        public void setOnItemClick(OnItemClick onItemClick) {
            this.onItemClick = onItemClick;
        }

        public void bindView(Object o) {
            cardView.setTag(o);
        }

        @Override
        public void onClick(View view) {
            onItemClick.OnClick(view, view.getTag(), getLayoutPosition());
        }
    }


    public class FooterHoder extends RecyclerView.ViewHolder {

        public FooterHoder(View itemView) {
            super(itemView);
        }
    }

    public interface OnItemClick {
        void OnClick(View view, Object o, int position);
    }
}




